#include "DynLibManager.h"
#include "DynLib.h"

CDynLibManager::CDynLibManager()
{
}

CDynLib* CDynLibManager::Load( const std::string & filename)
{
    DynLibList::iterator i = m_LibList.find(filename);
    if (i != m_LibList.end())
    {
        return i->second;
    }
    else
    {
        CDynLib* pLib = new CDynLib(filename);
        pLib->Load();        
        m_LibList[filename] = pLib;
        return pLib;
    }
}
//-----------------------------------------------------------------------
void CDynLibManager::Unload(CDynLib* lib)
{
    DynLibList::iterator i = m_LibList.find(lib->GetName());
    if (i != m_LibList.end())
    {
        m_LibList.erase(i);
    }
    lib->Unload();
    delete lib;
}
//-----------------------------------------------------------------------
CDynLibManager::~CDynLibManager()
{
    // Unload & delete resources in turn
    for( DynLibList::iterator it = m_LibList.begin(); it != m_LibList.end(); ++it )
    {
        it->second->Unload();
        delete it->second;
    }

    // Empty the list
    m_LibList.clear();
}
